﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.BusinessObjects;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.Common;

namespace SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar
{
    [TemplatePart(Name = "PART_AppointmentPanel", Type = typeof(AppointmentItemsPanel))]
    public class ucWeekContent : ucItemsControl
    {
        #region Variables

        private ScrollViewer svContent = null;
        private AppointmentItemsPanel AppointmentPanel = null;

        #endregion Variables

        #region Constructor

        public ucWeekContent()
        {
            DefaultStyleKey = typeof(ucWeekContent);
            this.Loaded += new RoutedEventHandler(ucWeekContent_Loaded);
        }

        #endregion Constructor

        #region Methods

        void ucWeekContent_Loaded(object sender, RoutedEventArgs e)
        {
            ScrollToCurrentTime();
        }

        public void ScrollToCurrentTime()
        {
            if (svContent == null) return;
            ucTimeSlots timeSlots = (ucTimeSlots)base.GetTemplateChild("PART_TimeSlots");
            if (timeSlots != null && timeSlots.Items.Count > 0)
            {
                ucTimeSlotItem currentTimeItem = (ucTimeSlotItem)(timeSlots).Items[0];
                double i = currentTimeItem.Height;
                svContent.ScrollToVerticalOffset(i * DateTime.Now.Hour);
            }
        }

        #region OnApplyTemplate

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            svContent = (ScrollViewer)base.GetTemplateChild("svContent");

            if (svContent == null) return;

            ScrollViewerExtensions.SetIsMouseWheelScrollingEnabled(svContent, true);

            AppointmentPanel = (AppointmentItemsPanel)base.GetTemplateChild("PART_AppointmentPanel");

            if (AppointmentPanel == null) return;

            AppointmentPanel.Owner = this;

            int DayOffSet = 5;

            if (Owner.CurrentDateView == Enumerations.DateView.FullWeek)
                DayOffSet = 7;

            DateTime _MondaysDate = Owner.CurrentDate.AddDays((int)System.DayOfWeek.Monday - (int)Owner.CurrentDate.DayOfWeek);
            DateTime _EndOfWeekDate = _MondaysDate.AddDays(DayOffSet);

            foreach (Appointment app in Owner.Appointments)
            {
                if (app.StartDate.Value.Date >= _MondaysDate.Date && app.StartDate.Value.Date <= _EndOfWeekDate.Date)
                {
                    ucAppointmentItem _Item = new ucAppointmentItem(AppointmentPanel);
                    _Item.Background = new SolidColorBrush(Colors.Green);
                    _Item.StartDate = app.StartDate.Value;
                    _Item.EndDate = app.EndDate.Value;
                    _Item.StartTime = SLControlLibrary.Resources.Common.Utility.RoundTime(app.StartTime, Owner.TimeSlotsPerHour);
                    _Item.EndTime = SLControlLibrary.Resources.Common.Utility.RoundTime(app.EndTime, Owner.TimeSlotsPerHour);
                    _Item.Desc = app.Desc;

                    if (app.Value != null)
                    {
                        if (app.Value.Color != null)
                            _Item.AppointmentBackground = app.Value.Color;
                        else
                            _Item.AppointmentBackground = Owner.DayAppointmentBackground;
                    }
                    else
                        _Item.AppointmentBackground = Owner.DayAppointmentBackground;

                    _Item.AppointmentForeground = Owner.DayAppointmentForeground;
                    _Item.AppointmentBorderBrush = Owner.DayAppointmentBorderBrush;
                    _Item.AppointmentBorderThickness = Owner.DayAppointmentBorderThickness;

                    AppointmentPanel.Margin = new Thickness(52, 0, 0, 0);
                    AppointmentPanel.Children.Add(_Item);
                }
            }
        }

        #endregion OnApplyTemplate

        #endregion Methods
    }
}